<template>
    <ul class="app-header-nav">
        <li class="home">
            <RouterLink to="/">首页</RouterLink>
        </li>
        <!--一级分类-->
        <li v-for="category of categoryList" :key="category.id" @mouseenter="show(category)" @mouseleave="hide(category)">
            <!-- <a href="#">{{ category.name }}</a>-->
            <RouterLink :to="`/category/${category.id}`" @click="hide(category)">{{ category.name }}</RouterLink>
            <!--由于是单页面路由跳转不会刷新页面，css的hover一直触发无法关闭分类弹窗-->
            <div class="layer" :class="{open:category.open}">
                <!--二级分类-->
                <ul>
                    <li v-for="sub of category.children" :key="sub.id">
                        <RouterLink :to="`/category/sub/${sub.id}`" @click="hide(category)">
                            <img :src="sub.picture" alt="">
                            <p>{{ sub.name }}</p>
                        </RouterLink>
                    </li>
                </ul>
            </div>
        </li>
    </ul>
</template>

<script>
import { useStore } from "vuex";
import { computed } from "vue";
/*
* 头部导航组件
* */
export default {
    name: "AppHeaderNav",
    setup(){
        const store = useStore();
        // const categoryList = store.state;
        // console.log("categoryList", categoryList);

        const categoryList = computed(() => {
            return store.state.category.list;
        });

        const show = (item) => {
            store.commit("category/show", item);
        };
        const hide = (item) => {
            store.commit("category/hide", item);
        };
        return {
            categoryList,
            show,
            hide
        };
    }
};
</script>

<style lang="less" scoped>

// 一级菜单样式
.app-header-nav {
    width: 820px;
    display: flex;
    justify-content: space-around;
    padding-left: 40px;
    position: relative;

    > li {
        margin-right: 40px;
        width: 38px;
        text-align: center;

        > a {
            font-size: 16px;
            line-height: 32px;
            height: 32px;
            display: inline-block;
        }

        &:hover {
            > a {
                color: @xtxColor;
                border-bottom: 1px solid @xtxColor;
            }

            // 显示二级类目
            > .layer {
                //height: 132px;
                //opacity: 1;
                &.open {
                    height: 132px;
                    opacity: 1;
                }
            }

        }
    }
}

// 二级菜单样式
.layer {
    width: 1240px;
    background-color: #fff;
    position: absolute;
    left: -200px;
    top: 56px;
    height: 0;
    overflow: hidden;
    opacity: 0;
    box-shadow: 0 0 5px #ccc;
    transition: all .2s .1s;

    ul {
        display: flex;
        flex-wrap: wrap;
        padding: 0 70px;
        align-items: center;
        height: 132px;

        li {
            width: 110px;
            text-align: center;

            img {
                width: 60px;
                height: 60px;
            }

            p {
                padding-top: 10px;
            }

            &:hover {
                p {
                    color: @xtxColor;
                }
            }
        }
    }
}
</style>
